/**
 * The contents of this file are subject to the license and copyright detailed in the LICENSE and NOTICE files at the root of the source tree and available online at
 * http://www.askosi.org/license.html
 */
package be.destin.skos.core;

import java.util.LinkedList;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;

import be.destin.rdf.changes.Status;
import be.destin.rdf.changes.StatusInterface;

public class LinkedConcept extends ConceptIdentifier implements StatusInterface {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	Status status;
	String relationQualifier; // TODO: none, Generic, Partitive, Instantial, custom... TO CONTROL USING AN AD-HOC SKOS SCHEME!!!
	
	public LinkedConcept() { // For JAXB...
		
	}

	public LinkedConcept (Concept context, LocalLinkedConcept source) {
		relAbout = context.getInScheme().getAbout_About(source.relAbout);
		status = source.status;
		relationQualifier = source.relationQualifier;
	}

	public LinkedConcept (String anAbout, Status aStatus, String subRel) {
		super(anAbout);
		status = aStatus;
		relationQualifier = subRel;
	}
	
	public LinkedConcept getThis() {
		return this;
	}

	public LinkedConcept clean() {
		if (relAbout == null) return null;
		String anAbout = SkosUtil.normalizeAbout(relAbout);
		if (anAbout.isEmpty()) return null;
		String aQualifier = null;
		if (relationQualifier != null && !relationQualifier.isEmpty()) {
			aQualifier = SkosUtil.normalizeAbout(relationQualifier);
		}
		return new LinkedConcept(anAbout,status,aQualifier);
	}
	
	@XmlAttribute(name="status")
	public String getStatus() {
		if (status == null) return null;
		return status.toString();
	}

	public void setStatus(String aStatus) {
		if (aStatus == null || aStatus.isEmpty()) {
			this.status = null;
			return;
		}
		if (aStatus.startsWith("status_")) {
			aStatus = aStatus.substring("status_".length());
		}
		this.status = Status.valueOf(aStatus);
	}

	@XmlTransient
	public Status getCurrentStatus() {
		return status;
	}

	public void setCurrentStatus(Status status) {
		if (status == null) return;
		this.status = status;
	}

	@XmlAttribute(name="qual")
	public String getRelationQualifier() {
		return relationQualifier;
	}

	public void setRelationQualifier(String subRelation) {
		this.relationQualifier = subRelation;
	}

	public static LinkedList<String> getScheme_About(LinkedList<LinkedConcept> rels) {
		LinkedList<String> result = new LinkedList<String>();
		if (rels == null) return result;
		for (LinkedConcept aRel : rels) {
			if (aRel.getCurrentStatus() == Status.Not_Approved) continue;  // Do not include rejected relations
			result.add(aRel.getAbout());
		}
		return result;
	}

	public String toRdfObjectUrl() {
		return "&rel="+this.getAbout()+"&relqualifier="+this.getRelationQualifier();
	}

	public String toJavaScriptParameters() {
		return ",rel:'"+this.getAbout()+"',relqualifier:'"+this.getRelationQualifier()+"'";
	}

	public static LinkedList<LinkedConcept> cleanLinks (LinkedList<LinkedConcept> toBeCleaned) {
		if (toBeCleaned == null) return null;
		LinkedList<LinkedConcept> result = new LinkedList<LinkedConcept>();
		for (LinkedConcept anEntry : toBeCleaned) {
			if (anEntry != null) {
				LinkedConcept cleanEntry = anEntry.clean();
				if (cleanEntry != null) {
					result.add(cleanEntry);
				}
			}
		}
		return result;
	}

	public static void mergeLinks ( LinkedList<LinkedConcept> currRels, LinkedList<LinkedConcept> toBeCleaned) {
		if (toBeCleaned == null) return;
		if (currRels == null) currRels = new LinkedList<LinkedConcept>();
		for (LinkedConcept anEntry : toBeCleaned) {
			if (anEntry != null) {
				LinkedConcept cleanEntry = anEntry.clean();
				if (cleanEntry != null) {
					int pos = currRels.indexOf(cleanEntry);
					if (pos >= 0) {
						LinkedConcept existingRel = currRels.get(pos);
						existingRel.setCurrentStatus(cleanEntry.getCurrentStatus());
						String qual = cleanEntry.getRelationQualifier();
						if (qual != null && !qual.isEmpty()) {
							existingRel.setRelationQualifier(qual);
						}
					} else {
						currRels.add(cleanEntry);
					}
				}
			}
		}
	}
	
}
